from flask import Flask
from app.config import Config
from app.extensions import db, migrate, api
from app.apis.auth import auth_ns
from app.apis.user import user_ns
from app.protocols import base_ns
from app.middlewares.auth_middleware import auth_middleware

def create_app():
    app = Flask(__name__)
    app.config.from_object(Config)
    app.debug = True

    # 初始化
    db.init_app(app)
    migrate.init_app(app, db)
    api.init_app(app)

    # 注册中间件
    @app.before_request
    def before_request():
        auth_middleware()

    # 注册命名空间
    api.add_namespace(auth_ns)
    api.add_namespace(user_ns)
    api.add_namespace(base_ns)

    return app
